package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.User;
import cn.pconline.officialclub.entity.OClub;
import cn.pconline.officialclub.service.OClubService;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author chensy
 */
public class CensorIntroduceService extends CensorService {

    /**
     * 车友会简介typeId,在bean初始化的时候赋值
     */
    private long typeIdClubIntroduce;
    @Autowired
    private UserService userService;
    @Autowired
    private ClubService clubService;
    @Autowired
    private OClubService oClubService;
    public void setTypeIdClubIntroduce(long typeIdClubIntroduce) {
        this.typeIdClubIntroduce = typeIdClubIntroduce;
    }

    /**
     * 审核应用回馈
     *
     * @param interfaceUrl
     * @param url
     * @param status
     * @param version
     * @return
     */
    public String feedback(String interfaceUrl, String url, int status,
            long version) {
        String result = null;
        // url must format like : http://my.pcauto.com.cn/2?typeId=203
        Map<String,Long> map  = getMapByUrl(url);
        long clubId = map.get("clubId");
        long typeId = map.get("typeId");
        if (typeId == typeIdClubIntroduce) {
            Club club = clubService.getClub(clubId);
            if (club!=null&&isTheSameVersion(version, club.getUpdateAt().getTime())) {
                switch (status) {
                    //审核通过不处理
                    case CENSORSYSTEM_RESPONSE_PASS:
                        break;
                    //审核不通过    
                    case CENSORSYSTEM_RESPONSE_DENY:
                        club.setIntroduce("内容含有敏感词，已屏蔽");
                        geliDao.update(club,"introduce");
                        log.info("车友会简介审核未通过：" + clubId);
                        break;
                    default:
                        result = "状态不正确";
                        break;
                }
            }else{
            	log.info("审核平台回调车友会已不存在:"+clubId);
            }
        }

        return result;
    }
    /**
     * 品牌车友会审核应用回馈
     *
     * @param interfaceUrl
     * @param url
     * @param status
     * @param version
     * @return
     */
    public String feedbackOfficialClub(String interfaceUrl, String url, int status,
            long version) {
        String result = null;
        // url must format like : http://my.pcauto.com.cn/2?typeId=203
        Map<String,Long> map  = getMapByUrl(url);
        long clubId = map.get("clubId");
        long typeId = map.get("typeId");
        if (typeId == typeIdClubIntroduce) {
            OClub club = oClubService.findById(OClub.class,clubId);
            if (club!=null&&isTheSameVersion(version, club.getUpdateAt().getTime())) {
                switch (status) {
                    //审核通过不处理
                    case CENSORSYSTEM_RESPONSE_PASS:
                        break;
                    //审核不通过    
                    case CENSORSYSTEM_RESPONSE_DENY:
                        club.setIntroduce("内容含有敏感词，已屏蔽");
                        geliDao.update(club,"introduce");
                        log.info("车友会简介审核未通过：" + clubId);
                        break;
                    default:
                        result = "状态不正确";
                        break;
                }
            }else{
            	log.info("审核平台回调车友会已不存在:"+clubId);
            }
        }

        return result;
    }
    /**
     * 创建车友会简介时,首次通知审核平台新建车友会简介状态
     *
     * @param request
     * @param club
     */
    public void notifyCensorSystemCreate(HttpServletRequest request, final Club club) {
        if (csr_switch) {
            User user = userService.findUser(club.getCreateBy());
            final Map<String, Object> params = convert4Create(request, user, club.getClubId(), typeIdClubIntroduce, club.getIntroduce(), club.getUpdateAt().getTime(), true,CLUB_TYPE_COMMON);
            executorService.submit(new CensorCall(receiveHome, params));
        }
    }
     /**
     * 创建车友会简介时,首次通知审核平台新建车友会简介状态
     *
     * @param request
     * @param club
     */
    public void notifyCensorSystemCreate(HttpServletRequest request, final OClub club) {
        if (csr_switch) {
            User user = userService.findUser(club.getCreateBy());
            final Map<String, Object> params = convert4Create(request, user, club.getoClubId(), typeIdClubIntroduce, club.getIntroduce(), club.getUpdateAt().getTime(), true,CLUB_TYPE_OFFICIAL);
            executorService.submit(new CensorCall(receiveHome, params));
        }
    }

    @Override
    public boolean isCorrectFeedback(String url) {
    	Map<String,Long> map  = getMapByUrl(url);
    	long typeId = 0;
    	if(map!=null&&!map.isEmpty()){
    		typeId = map.get("typeId");
    	}
        return typeIdClubIntroduce == typeId;
    }
}
